package inutan.gateway.session.user.hook;

import inutan.gateway.external.ExternalKit;
import inutan.common.inner.protocol.InnerMessage;
import inutan.gateway.session.LogicSessions;
import inutan.gateway.session.user.UserSession;
import inutan.gateway.session.user.UserSessions;
import org.ricks.common.lang.Logger;
import org.ricks.net.Router;

/**
 * UserHookDefault
 * 改造成抽象的钩子，quit逻辑要销毁掉自己的 cache db回写容器
 * 异步回写还是用回以 table-name 机制，玩家退出不用销毁回写容器，只用clear玩家缓存
 */
public class UserHookDefault implements UserHook {

    @Override
    public void into(UserSession userSession) {
        long userId = userSession.getUserId();
        Logger.info("玩家上线 userId: "+userId+" -- " +  userSession.getUserChannelId());
        Logger.info("当前在线玩家数量： "+  UserSessions.me().countOnline());
    }

    @Override
    public void quit(UserSession userSession) {
        long userId = userSession.getUserId();
        Logger.info("玩家退出 userId: "+userId+ " -- "+ userSession.getUserChannelId());
        Logger.info("当前在线玩家数量： "+ UserSessions.me().countOnline());
        /**          通知业务节点，数据刷入db销毁异步回写容器&清除缓存         */
        InnerMessage innerMessage = ExternalKit.createInnerMessage((short) 5000);
        innerMessage.userId = userId;
        innerMessage.userSessionId = userSession.getUserChannelId();
//        LogicSessions.me().route(innerMessage,userSession); //玩家退出
        LogicSessions.me().syncAsk(innerMessage, userSession, askPackage -> {
            Logger.info("玩家退出游戏成功，开始销毁UserSession对象 ");
            UserSessions.me().remove(userSession); //解除UserSession 分组
        });
    }
}
